import Vue from 'vue'
import VueRouter from 'vue-router'
// import Home from '../views/Home.vue'

Vue.use(VueRouter)

// const routes = [
//   {
//     path: '/',
//     name: 'home',
//     component: Home
//   },
//   {
//     path: '/about',
//     name: 'about',
//     // route level code-splitting
//     // this generates a separate chunk (about.[hash].js) for this route
//     // which is lazy-loaded when the route is visited.
//     component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
//   }
// ]

// const router = new VueRouter({
//   mode: 'history',
//   base: process.env.BASE_URL,
//   routes
// })

//引入组件
import Home from '../pages/home';
import List from '../pages/list';
import Cart from '../pages/cart';
import Mine from '../pages/mine';
import List2 from '../pages/list2';
import Details from '../pages/details';
import Sousuo from '../pages/sousuo';
import Login from '../pages/login';
import Reg from '../pages/reg';
import axios from 'axios';


//实例化router并配置参数
let router = new VueRouter({
    routes:[
        {
            name:'home', //路由名
            path:'/home',   //路由路径
            component:Home //组件名
        },{
            name:'list',
            path:'/list',
            component:List
        },{
            name:'cart',
            path:'/cart',
            component:Cart,
            meta: {
                ispower: true //进入购物车需要鉴权
            }
        },{
            name:'mine',
            path:'/mine',
            component:Mine,
            meta: {
                ispower: true //进入购物车需要鉴权
            }
        },
        {
            name:'details',
            path:'/details',
            component:Details
        },{
            name:'list2',
            path:'/list2',
            component:List2
        },{
            name:'sousuo',
            path:'/sousuo',
            component:Sousuo
        },{
            name:'login',
            path:'/login',
            component:Login
        },{
            name:'reg',
            path:'/reg',
            component:Reg
        }
    ]
});

router.beforeEach(async (to,from,next)=>{
    if(to.meta.ispower){
        //需要鉴权  获取token
        let token = localStorage.getItem('token');
        let {data} = await axios.get("http://localhost:5555/listbo/verify",{params:{token}}) //把token发送到后端验证
        if(data.type){
            window.console.log("已登录可以进入该路由")
            next()
        }else{
            router.push({path:'/login',tageturl:to.path})
        }
    }else{ //不需要鉴权
        next()
    }
})


export default router
